package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.math.BigInteger;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import edu.hawaii.myisern.collaborations.jaxb.CollaboratingOrganizations;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.collaborations.jaxb.CollaborationTypes;
import edu.hawaii.myisern.collaborations.jaxb.OutcomeTypes;
import edu.hawaii.myisern.collaborations.jaxb.Years;
import edu.hawaii.myisern.organizations.jaxb.AffiliatedResearchers;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.organizations.jaxb.ResearchKeywords;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Demonstrates that the MyIsernSanityCheck is working.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author Jianfei Liao
 * @author Jared Sunouchi
 */
public class TestMyIsernSanityCheck {
  /** The test object instance. */
  private MyIsernModel myIsernModel = MyIsernModel.getInstance();
  /** The Sanity Check instance. */
  MyIsernSanityCheck checker;
  /** A string for "user.dir". */
  private static final String USER_DIR = "user.dir";
  /** A string for "Checking errors entry size". */
  private static final String CHECK_ERROR_SIZE = "Checking errors entry size";
  /** A string for "Checking ErrorFrom entry". */
  private static final String CHECK_ERROR_FROM = "Checking ErrorFrom entry";
  /** A string for "Checking ErrorAt entry". */
  private static final String CHECK_ERROR_AT = "Checking ErrorAt entry";
  /** A string for "Checking ErrorMessage entry". */
  private static final String CHECK_ERROR_MSG = "Checking ErrorMessage entry";
  /** A string for " field is empty or invalid format. ". */
  private static final String EMPTY_FIELD = " field is empty or invalid format. ";
 
  /**
   * Initialization before the tests start.
   */
  @Before
  public void initilize() {
    String directory = System.getProperty(USER_DIR) + "/xml/test";
    // export the current data for backup
    boolean isExported = myIsernModel.exportXml(
        System.getProperty(USER_DIR) + "/researchers.tmp.xml", 
        System.getProperty(USER_DIR) + "/organizations.tmp.xml",
        System.getProperty(USER_DIR) + "/collaborations.tmp.xml");
    assertTrue("Checking data export", isExported);
    
    // import the test data
    boolean isImported = myIsernModel.importResearchersXml(directory + "/researchers.test.xml");
    assertTrue("Checking researchers XML file import", isImported);
    isImported = myIsernModel.importOrganizationsXml(directory + "/organizations.test.xml");
    assertTrue("Checking organizations XML file import", isImported);
    isImported = myIsernModel.importCollaborationsXml(directory + "/collaborations.test.xml");
    assertTrue("Checking collaborations XML file import", isImported);
    
    // create the sanity check
    checker = new MyIsernSanityCheck(this.myIsernModel);
  }
  
  /**
   * Test the invalid or empty organization in a researcher.
   */
  @Test
  public void testOrganizationInResearcher() {
    // add a researcher with invalid organization
    Researcher researcher = new Researcher();
    researcher.setName("New Researcher");
    this.myIsernModel.addOrEditResearcher(researcher);
    // check if the sanity check catch the empty organization error
    List<SanityError> errors = checker.emptyOrganizationInResearcher();
    assertEquals(CHECK_ERROR_SIZE, 1, errors.size());
    assertEquals(CHECK_ERROR_AT, "Organization", errors.get(0).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, "New Researcher", errors.get(0).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, EMPTY_FIELD, 
        errors.get(0).getErrorMessage());
    
    // set the organization to an invalid one
    researcher.setOrganization("No Such Organization");
    this.myIsernModel.addOrEditResearcher(researcher);
    // check if the sanity check catch the invalid organization error
    errors = checker.invalidOrganizationInResearcher();
    assertEquals(CHECK_ERROR_SIZE, 1, errors.size());
    assertEquals(CHECK_ERROR_AT, "No Such Organization", errors.get(0).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, "New Researcher", errors.get(0).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, " is not in the database. ", 
        errors.get(0).getErrorMessage());
  }
  
  /**
   * Test the invalid or empty affiliated researchers in an organization.
   */
  @Test
  public void testAffiliatedResearchersInOrganization() {
    // the test data already contain empty affiliated researchers fields
    List<SanityError> errors = checker.emptyAffiliatedResearchersInOrganization();
    assertEquals(CHECK_ERROR_SIZE, 2, errors.size());
    assertEquals(CHECK_ERROR_AT, "Affiliated Researchers", errors.get(0).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, "University of Hawaii", errors.get(0).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, EMPTY_FIELD, 
        errors.get(0).getErrorMessage());
    assertEquals(CHECK_ERROR_AT, "Affiliated Researchers", errors.get(1).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, "Avaya Labs Research", errors.get(1).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, EMPTY_FIELD, 
        errors.get(1).getErrorMessage());
    
    // add an organization with invalid affiliated researchers
    Organization org = new Organization();
    org.setName("New Org");    
    // set the affiliated researchers to an invalid one
    AffiliatedResearchers ar = new AffiliatedResearchers();
    ar.getAffiliatedResearcher().add("Some One");
    ResearchKeywords rk = new ResearchKeywords();
    org.setAffiliatedResearchers(ar);
    org.setResearchKeywords(rk);
    this.myIsernModel.addOrEditOrganization(org);
    
    // check if the sanity check catch the invalid affiliated researchers error
    errors = checker.invalidAffiliatedResearchersInOrganization();
    assertEquals(CHECK_ERROR_SIZE, 1, errors.size());
    assertEquals(CHECK_ERROR_AT, "Some One", errors.get(0).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, "New Org", errors.get(0).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, " is not in the database. ", errors.get(0).getErrorMessage());
  }
  
  /**
   * Test the invalid or empty collaborating organizations, and invalid years in a collaboration.
   */
  @Test
  public void testCollaboration() {
    // add a collaboration with empty collaborating organizations and invalid years
    Collaboration collab = new Collaboration();
    String name = "New Collab";
    collab.setName(name);
    CollaboratingOrganizations collabOrgs = new CollaboratingOrganizations();
    collab.setCollaboratingOrganizations(collabOrgs);
    CollaborationTypes collabTypes = new CollaborationTypes();
    collab.setCollaborationTypes(collabTypes);
    Years years = new Years();
    years.getYear().add(new BigInteger("3000"));
    collab.setYears(years);
    OutcomeTypes outcome = new OutcomeTypes();
    collab.setOutcomeTypes(outcome);
    this.myIsernModel.addOrEditCollaboration(collab);
    
    // check if the sanity check catch the empty collaborating organizations error
    List<SanityError> errors = checker.emptyOrganizationsInCollaboration();
    assertEquals(CHECK_ERROR_SIZE, 1, errors.size());
    assertEquals(CHECK_ERROR_AT, "Collaborating Organizations", errors.get(0).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, name, errors.get(0).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, EMPTY_FIELD, 
        errors.get(0).getErrorMessage());
    
    // set the collaborating organizations to an invalid one
    collabOrgs.getCollaboratingOrganization().add("No Such Collab Org");
    this.myIsernModel.addOrEditCollaboration(collab);
    
    // check if the sanity check catch the new collaborating organizations error
    errors = checker.invalidOrganizationsInCollaboration();
    assertEquals(CHECK_ERROR_SIZE, 4, errors.size());
    assertEquals(CHECK_ERROR_AT, "No Such Collab Org", errors.get(3).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, name, errors.get(3).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, " is not in the database. ", errors.get(3).getErrorMessage());
    
    // check if the sanity check catch the invalid years error
    errors = checker.invalidYearsInCollaboration();
    assertEquals(CHECK_ERROR_SIZE, 1, errors.size());
    assertEquals(CHECK_ERROR_AT, "3000", errors.get(0).getErrorAt());
    assertEquals(CHECK_ERROR_FROM, name, errors.get(0).getErrorFrom());
    assertEquals(CHECK_ERROR_MSG, 
        " is not in proper range (1990 to 2010). ", errors.get(0).getErrorMessage());
  }
  
  /**
   * Recover the data after testing.
   */
  @After
  public void recover() {
    // import from the tmp files to restore the data
    boolean isImported = myIsernModel.
        importResearchersXml(System.getProperty(USER_DIR) + "/researchers.tmp.xml");
    assertTrue("Checking researchers XML file import", isImported);
    isImported = myIsernModel.
        importOrganizationsXml(System.getProperty(USER_DIR) + "/organizations.tmp.xml");
    assertTrue("Checking organizations XML file import", isImported);
    isImported = myIsernModel.
        importCollaborationsXml(System.getProperty(USER_DIR) + "/collaborations.tmp.xml");
    assertTrue("Checking collaborations XML file import", isImported);
    
    // delete the tmp files
    File file = new File(System.getProperty(USER_DIR) + "/researchers.tmp.xml");
    boolean isDeleted = file.delete();
    assertTrue("Checking researchers tmp file deletion", isDeleted);
    file = new File(System.getProperty(USER_DIR) + "/organizations.tmp.xml");
    file.delete();
    assertTrue("Checking organizations tmp file deletion", isDeleted);
    file = new File(System.getProperty(USER_DIR) + "/collaborations.tmp.xml");
    file.delete();
    assertTrue("Checking collaborations tmp file deletion", isDeleted);
  }
}